<?php

namespace controller;

/**
 * Description of ShopAdmin_Opinion
 *
 * @author Damian
 */
class ShopAdmin_Opinion extends Seller {
    public function index() {
        if(\library\RequestMethods::post("OpinionID")){
             $opinion = \model\TblOpinion::getModel()->getByPrimaryKey(\library\RequestMethods::post("OpinionID"));
            $opinion->Visable = (int)\library\RequestMethods::post("visable");
            $opinion->Moderated = (int)\library\RequestMethods::post("moderated");
            $opinion->save();
        }
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Lista komentarzy");
        $opinionsCriteria = new \model\Criteria();
        $opinionsCriteria->from(\model\TblOpinion::getModel()->getTable())
                ->join(\model\TblCustomer::getModel())
                ->join(\model\TblPerson::getModel())
                ->join(\model\TblProduct::getModel())
                ->order(\model\TblOpinion::ADDITION_DATE, " desc ",  \model\TblOpinion::getModel()->getTable())
                ->where(\model\TblOpinion::SHOP_ID, "=", $this->shop->ShopID, \model\TblOpinion::getModel()->getTable());
        $page = (\library\RequestMethods::get("Page")) ? \library\RequestMethods::get("Page") : 1;
        $session = \library\Registry::get("session");
         if (\library\RequestMethods::get("order")) {
            $session->set("orderOpinions", \library\RequestMethods::get("order"));
        }
        $view = $this->getActionView();
        $view->set("order", $session->get("orderOpinions"));
        $order = \model\TblOpinion::createOrderString($session->get("orderOpinions"));
        $opinionsCriteria->order($order[0], $order[1], $order[2]);
        if (\library\RequestMethods::get("id")) {
            $opinionsCriteria->where(\model\TblOpinion::ID, "=", \library\RequestMethods::get("id"), \model\TblOpinion::getModel()->getTable(), " and ");
        }
        $all = $opinionsCriteria->count();
        $pagesCount = ceil($all / $this->perPage);
        $pages = new \view\Pagination($page, 1, $pagesCount, "./" . $this->shop->URL . "/" . "ShopAdmin_Order");
        $layoutView->set("pages",$pages);
        $opinions = \model\TblOrder::getModel()->all($opinionsCriteria->limit($this->perPage,$page));
        $opinionsDataTable = new \model\DataTable($opinions);
        $opinionsTable = $opinionsDataTable->getModelData(true);
        
        $view->set("opinions",$opinionsTable);
        $this->render();
    }

}
